<?php
namespace API\Controller;
use Think\Controller;
/**
* 关注逻辑
*/
/*
	1,文章 ；2，用户 ；3，电影；
	 */
class CollectController extends Controller
{
	
	/**
	 * 添加关注/取消关注
	 * @return [type] [description]
	 */
	public function addcollect(){
		if(IS_POST){
			$data['u_id'] = I('post.u_id');
			$data['collect_flag'] = I('post.collect_flag');
			$data['collect_id'] = I('post.collect_id');
		}
		elseif (IS_GET) {
			$data['u_id'] = I('get.u_id');
			$data['collect_flag'] = I('get.collect_flag');
			$data['collect_id'] = I('get.collect_id');
		}
		if(!empty($data['u_id'])&&!empty($data['collect_flag'])&&!empty($data['collect_id'])){
				$colect = M('collect');
				$state = $colect -> where($data) -> find();
				if(empty($state['col_id'])){
					/**
					 * 添加关注
					 */
					$data['col_state'] = "1";
					$data['col_time'] = array('exp','now()');
					$res = $colect -> add($data);
					$result = $res ? 1 : 0;
					if($result){
						$sent = array("success"=>1);
						echo json_encode($sent);
					}else{
						$sent = array("success"=>0);
						echo json_encode($sent);
					}
				}else{
					/**
					 * 取消关注
					 */
					if(false!=$colect->where('col_id ='.$state['col_id'])->delete()){
						$sent = array("success"=>1);
						echo json_encode($sent);
					}else{
						$sent = array("success"=>0);
						echo json_encode($sent);
					}
				}

		}else{
			    $sent = array("success"=>404);
				echo json_encode($sent);
		}
	}



	/**
	 * 
	 * @return [type] [description]
	 */
	public function collectstate(){
			if(IS_POST){
			$data['u_id'] = I('post.u_id');
			$data['collect_flag'] = I('post.collect_flag');
			$data['collect_id'] = I('post.collect_id');
		}
		elseif (IS_GET) {
			$data['u_id'] = I('get.u_id');
			$data['collect_flag'] = I('get.collect_flag');
			$data['collect_id'] = I('get.collect_id');
		}
		if(!empty($data['u_id'])&&!empty($data['collect_flag'])&&!empty($data['collect_id'])){
				$colect = M('collect');
				$num = $colect -> where($data) -> count();
				if(0 == $num){
					/**
					 * 未关注
					 * 英语不太好随便起名字了
					 */
					$sent = array("success"=>0,"msg"=>"未关注");
					echo json_encode($sent);

				}
				else{
					/**
					 * 已关注
					 */
					$sent = array("success"=>1,"msg"=>"已关注");
					echo json_encode($sent);
				}
			}else{
				    $sent = array("success"=>404,"msg"=>" ");
					echo json_encode($sent);
			}
	}

	/*
	我的关注
	 */
	public function mycollect(){
		
		/** 1,文章 ；2，用户 ；3，电影； */
		if(IS_POST){
		    $data['u_id'] = I('post.u_id');
			$data['collect_flag'] = I('post.collect_flag');
		}
		elseif (IS_GET) {
			$data['u_id'] = I('get.u_id');
			$data['collect_flag'] = I('get.collect_flag');
		}
		if(!empty($data['u_id'])&&!empty($data['collect_flag'])){
			$att = M('collect');
			$collectlist = $att -> where($data) ->field('collect_id')-> select();
			$num = count($collectlist);
			if(0 == $num){
				exit;
			}
			else{
				/** PHP版本小于5.5 处理数组*/
				$que = array();
				foreach ($collectlist as $val) {
					 $que[]= $val['collect_id'];
				}
			/** 1,文章 ；2，用户 ；3，电影； */
			if (1 == $data['collect_flag']) {
				$articles = M('articles');
				$mycollect['a_id'] = array('in',$que);
				$articles_collect = $articles -> where($mycollect) -> select();
				echo json_encode($articles_collect);
			}
			elseif (2 == $data['collect_flag']) {
				# code...users   	t_id
				$users = M('users');
				$twh['u_id'] = array('in',$que);
				$users_collect = $users -> where($twh) -> select();
				echo json_encode($users_collect);
			}
			elseif (3 == $data['collect_flag']) {
				# code...movies   m_id
				$movies = M('movies');
				$mwh['m_id'] = array('in',$que);
				$movies_collect = $movies -> where($mwh) -> select();
				echo json_encode($movies_collect);
			}
			
		  }
		}else{
			exit;
		}
	}
}